package com.gree.changying.activity.mainactivity.framgent;


import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

import com.gree.changying.R;
import com.gree.changying.activity.base.BasePageFragment;
import com.gree.changying.activity.mainactivity.framgent.homefrm.tuijian.adapter.ViewPagerAdapter;
import com.gree.changying.activity.searchactivity.SearchResultActivity;
import com.gree.changying.server.bean.IntentKV;
import com.gree.changying.utils.KeyboardUtils;
import com.gree.changying.utils.StringUtil;
import com.gree.changying.view.AutoLineLayout;
import com.gree.changying.widget.tablayout.jtablayout.tab.Tab;
import com.gree.changying.widget.tablayout.widget.JTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchFrm extends BasePageFragment {
    @Override
    protected void initView(View view) {
        ButterKnife.bind(this,view);
        init();
        initHaoliao();
    }


    /**
     * 搜索相关
     */
    @Bind(R.id.searchVal)
    EditText searchVal;
    @Bind(R.id.searchValHit)
    TextView searchValHit;
    @Bind(R.id.clear)
    ImageView clear;
    @Bind(R.id.cancel)
    Button cancel;
    @OnClick({R.id.searchBox,R.id.searchValHit})
    public void searchBoxClick(){
        if(cancel.getVisibility()!=View.VISIBLE){
            cancel.setVisibility(View.VISIBLE);
            clear.setVisibility(View.VISIBLE);
            searchValHit.setVisibility(View.GONE);
            searchVal.setVisibility(View.VISIBLE);
            searchVal.post(() -> KeyboardUtils.showSoftKeyboard(searchVal));
        }
    }
    @OnClick(R.id.clear)
    public void clearClick(){
        searchVal.setText("");
    }
    @OnClick(R.id.cancel)
    public void cancelClick(){
        clearClick();
        cancel.setVisibility(View.GONE);
        clear.setVisibility(View.GONE);
        searchValHit.setVisibility(View.VISIBLE);
        searchVal.setVisibility(View.GONE);
        searchVal.post(() -> KeyboardUtils.hideSoftKeyboard(searchVal));
    }


    /**
     *  搜索相关
     */
    @Bind(R.id.haoliaoTab)
    JTabLayout haoliaoTab;
    @Bind(R.id.haoliaoPager)
    ViewPager haoliaoPager;
    ViewPagerAdapter haoliaoAdapter;
    private void initHaoliao(){
        haoliaoAdapter = new ViewPagerAdapter();
        haoliaoPager.setAdapter(haoliaoAdapter);
        haoliaoPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                haoliaoTab.selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        haoliaoTab.addOnTabSelectedListener(new JTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(@NonNull Tab var1) {
                haoliaoPager.setCurrentItem(var1.getPosition());
            }

            @Override
            public void onTabUnselected(@NonNull Tab var1) {

            }

            @Override
            public void onTabReselected(@NonNull Tab var1) {

            }
        });
        initHaoliaoData();
    }
    private String[] haoliao = {"方案","用户"};
    private String[] fenleid = {"足球","篮球"};
    private String[] shenglvd = {"50%以下","50%-70%","70%-90%","90%以上"};
    private String[] jifend = {"10以下","10-20","20-30","30-40","40以上"};
    private String[] history = {"测试","测试1"};
    private void initHaoliaoData(){
        List<View> views = new ArrayList<>();
        int i = 0 ;
        for(String s:haoliao){
            Tab tab = haoliaoTab.newTab().setTitle(s);
            tab.setTitleColor(getResources().getColor(R.color.tabHaoliao)
                    ,getResources().getColor(R.color.tabHaoliaoP));
            haoliaoTab.addTab(tab);
            View tView =  LayoutInflater.from(mContext).inflate(i==0?R.layout.view_fanan:R.layout.view_yonghu, null);
            views.add(tView);

            if(i==0){
                AutoLineLayout feilei = tView.findViewById(R.id.fenlei);
                for(String d:fenleid){
                    View vvv =  LayoutInflater.from(mContext).inflate(R.layout.view_labler, null);
                    TextView labler = vvv.findViewById(R.id.labler);
                    labler.setText(d);
                    feilei.addView(vvv);
                }
                AutoLineLayout shenglv = tView.findViewById(R.id.shenglv);
                for(String d:shenglvd){
                    View vvv =  LayoutInflater.from(mContext).inflate(R.layout.view_labler, null);
                    TextView labler = vvv.findViewById(R.id.labler);
                    labler.setText(d);
                    shenglv.addView(vvv);
                }
                AutoLineLayout jifen = tView.findViewById(R.id.jifen);
                for(String d:jifend){
                    View vvv =  LayoutInflater.from(mContext).inflate(R.layout.view_labler, null);
                    TextView labler = vvv.findViewById(R.id.labler);
                    labler.setText(d);
                    jifen.addView(vvv);
                }
                AutoLineLayout his = tView.findViewById(R.id.history);
                for(String d:history){
                    View vvv =  LayoutInflater.from(mContext).inflate(R.layout.view_history, null);
                    TextView labler = vvv.findViewById(R.id.labler);
                    labler.setText(d);
                    his.addView(vvv);
                }
            }else{
                AutoLineLayout shenglv = tView.findViewById(R.id.shenglv);
                for(String d:shenglvd){
                    View vvv =  LayoutInflater.from(mContext).inflate(R.layout.view_labler, null);
                    TextView labler = vvv.findViewById(R.id.labler);
                    labler.setText(d);
                    shenglv.addView(vvv);
                }
                AutoLineLayout his = tView.findViewById(R.id.history);
                for(String d:history){
                    View vvv =  LayoutInflater.from(mContext).inflate(R.layout.view_history, null);
                    TextView labler = vvv.findViewById(R.id.labler);
                    labler.setText(d);
                    his.addView(vvv);
                }
            }

            i++;
        }
        haoliaoAdapter.update(views);
        haoliaoPager.setCurrentItem(0);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.frm_search;
    }

    private void init(){
        searchVal.setOnEditorActionListener((textView, i, keyEvent) -> {
            if (i == EditorInfo.IME_ACTION_SEARCH) {
                if(StringUtil.isEmpty(searchVal.getText().toString())){
                    shortToast("请输入搜索内容!");
                    return false;
                }
                startActivity(SearchResultActivity.class, IntentKV.put("value",searchVal.getText().toString()));
            }
            return false;
        });
    }
}
